package cn.biao.test.client;

import cn.biao.test.tservice.HelloWorldService;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TConfiguration;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * @author lino
 */
@Slf4j
public class ThriftClient {
    private static final String HOST = "localhost";
    private static final int    PORT = 2345;
    private static final int    TIMEOUT = 3000;

    private void startClient(){
        TTransport transport = null;
        try {
            transport = new TSocket(new TConfiguration(), HOST, PORT, TIMEOUT);
            TProtocol protocol = new TBinaryProtocol(transport);
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);
            transport.open();
            String result = client.say("Lino");
            System.out.println(result);
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("Thrift Client 启动失败");
        } finally {
            if (null != transport){
                transport.close();
            }
        }
    }

    public static void main(String[] args) {
        ThriftClient thriftClient = new ThriftClient();
        thriftClient.startClient();
    }
}
